package com.imooc.ad.advice;

import com.imooc.ad.exception.AdException;
import com.imooc.ad.exception.MyException;
import com.imooc.ad.vo.CommonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**统一异常处理
 * Created by lzq on 2019/3/1
 */
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(value = AdException.class)
    public CommonResponse<String> handlerAdException(HttpServletRequest req,
                                                     AdException ex) {
        CommonResponse<String> response = new CommonResponse<>(-1,
                "business error");
        response.setData(ex.getMessage());
        return response;
    }

    @ExceptionHandler(value = MyException.class)
    public <T> CommonResponse<T> handlerMyException(HttpServletRequest req,
                                                     MyException ex) {
        return CommonResponse.createByErrorCodeMessage(ex.getCode(), ex.getMessage());
    }


}
